package com.lcredu.service.impl;

import com.lcredu.dao.FurnDAO;
import com.lcredu.dao.impl.FurnDAOImpl;
import com.lcredu.entity.Furn;
import com.lcredu.entity.Page;
import com.lcredu.service.FurnService;
import com.lcredu.utils.DefaultMsg;

import java.util.Collections;
import java.util.List;

/**
 * @author 黎长容
 * @date 2025-07-06 11:14
 */
public class FurnServiceImpl implements FurnService {
    private FurnDAO furnDAO = new FurnDAOImpl();

    @Override
    public List<Furn> getFurns() {
        return furnDAO.getFurnList();
    }

    @Override
    public int add(Furn furn) {
        return furnDAO.add(furn);
    }

    @Override
    public int deleteById(int id) {
        return furnDAO.del(id);
    }

    @Override
    public Furn getFurn(int furnId) {
        return furnDAO.selectFurnById(furnId);
    }

    @Override
    public Boolean updateFurn(Furn furn) {
        int updated = furnDAO.updateFurn(furn);
        return updated > 0;
    }

    @Override
    public Page<Furn> getPage(int pageNum, int pageSize) {

        //这里开始填充
        Page<Furn> page = new Page<>();
        //显示当前页
        page.setPageNum(pageNum);
        //每页显示几条记录
        page.setPageSize(pageSize);
        //共有多少条记录,通过 totalRow 和 pageSize ===> pageTotalCount
        int totalRow = furnDAO.getTotalRow();
        page.setTotalRow(totalRow);


        //表示该分多少页,通过计算得到?怎么得到?先获取多少行,再根据取模运算,可以得出可以分出多少页
        // 6 / 2 = 3
        // 5 / 2 = 2
        // 0 / 2 = 0;
        int pageTotalCount = totalRow / pageSize;
        if (totalRow % pageSize > 0) {
            pageTotalCount += 1;
        }
        page.setPageTotalCount(pageTotalCount);

        //表示当前页,要显示的数据
        int begin = (pageNum - 1) * pageSize;
        //pageNum = 1   pageSize = 3   => begin = 0    从第1条记录开始取
        //pageNum = 3   pageSize = 2   => begin = 4    从第5条记录开始取
        //有个坑
        List<Furn> pageItems = furnDAO.getItems(begin, pageSize);
        page.setItems(pageItems);

        //url->分页导航,待会取


//        //显示当前页
//        private Integer pageNum;
//
//        //每页显示几条记录
//        private Integer pageSize = DefaultMsg.DEFAULT_PAGE_SIZE.getPageSize();
//
//        //表示共有多少页,通过计算得到
//        private Integer pageTotalCount;
//
//        //共有多少条记录,通过 totalRow 和 pageSize ===> pageTotalCount
//        //DB可获取
//        private Integer totalRow;
//
//        //表示当前页,要显示的数据
//        //DB可获取
//        private List<T> items;
//
//        //分页导航条的字符串
//        private String url;
        return page;
    }

    @Override
    public Page<Furn> getPageByName(int pageNum, int pageSize, String name) {
        //这里填充数据
        Page<Furn> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        int totalRowByName = furnDAO.getTotalRowByName(name);
        page.setTotalRow(totalRowByName);
        //表示该分多少页,通过计算得到?怎么得到?先获取多少行,再根据取模运算,可以得出可以分出多少页
        int totalCount = totalRowByName / pageSize;
        if (totalRowByName % pageSize > 0) {
            totalCount += 1;
        }
        page.setPageTotalCount(totalCount);
        //从第几条数据开始查找
        int begin = (pageNum - 1) * pageSize;
        List<Furn> furnItemsByName = furnDAO.getItemsByName(begin, pageSize, name);
        page.setItems(furnItemsByName);
        return page;
    }
}
